package cn.sll.org.service.impl;

import cn.sll.base.query.BaseQuery;
import cn.sll.base.service.impl.BaseServiceImpl;
import cn.sll.base.util.MD5Utils;
import cn.sll.base.util.PageList;
import cn.sll.base.util.StrUtils;
import cn.sll.org.domain.Employee;
import cn.sll.org.mapper.EmployeeMapper;
import cn.sll.org.service.IEmployeeService;
import cn.sll.org.vo.EmpAndRoleVo;
import cn.sll.user.domain.Logininfo;
import cn.sll.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public List<Employee> findByDeptId(Long deptId) {
        return employeeMapper.findByDeptId(deptId);
    }

    @Override
    public void add(Employee employee) {
        String salt = StrUtils.getComplexRandomString(32);
        employee.setSalt(salt);
        employee.setPassword(MD5Utils.encrypByMd5(salt + employee.getPassword()));
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee, logininfo);
        logininfo.setType(0);
        logininfo.setDisable(true);
        logininfoMapper.add(logininfo);
        employee.setLogininfoId(logininfo.getId());

        super.add(employee);

        employeeMapper.addEmpRole(employee.getId(),employee.getRoleId());
    }

    @Override
    public void delete(Long id) {

        logininfoMapper.delete(employeeMapper.findById(id).getLogininfoId());

        employeeMapper.deleteEmpRole(id);

        super.delete(id);
    }

    @Override
    public void update(Employee employee) {
        Logininfo logininfo = logininfoMapper.findById(employee.getLogininfoId());
//        employee.setPassword(MD5Utils.encrypByMd5(employee.getSalt() + employee.getPassword()));
        BeanUtils.copyProperties(employee, logininfo);
        logininfoMapper.update(logininfo);

        super.update(employee);

        employeeMapper.deleteEmpRole(employee.getId());
        employeeMapper.addEmpRole(employee.getId(),employee.getRoleId());
    }

    @Override
    public PageList<Employee> queryByPage(BaseQuery query) {
        PageList<Employee> pageList = super.queryByPage(query);
        if (null == pageList || CollectionUtils.isEmpty(pageList.getRows())){
            return pageList;
        }
        // 我们要干的事：现在要将 员工对应的 角色id集合 添加到对象中
        // 根据empID查询到所有的role集合
        // 先去获取到所有的empid集合
        List<Employee> employeeList = pageList.getRows();
        List<Long> empIdList = employeeList.stream().map(Employee::getId).collect(Collectors.toList());
        // 根据 empIdList 去中间表中 查询 所有的roleId 集合
        List<EmpAndRoleVo> roles = employeeMapper.findRoles(empIdList);
        // 需要对vos 进行分组 返回一个map map的key：empId map的value：List<Long> roleIds
        Map<Long, List<Long>> map = roles.stream().collect(Collectors.groupingBy(EmpAndRoleVo::getEmployeeId,
                Collectors.mapping(EmpAndRoleVo::getRoleId, Collectors.toList())));

        employeeList.forEach(emp -> {
            List<Long> roleIds = map.get(emp.getId());
            emp.setRoleId(roleIds);
        });

        return pageList;
    }


}
